<?php
function printLoginAndLogoutStuff()
{
	if ($_REQUEST['librarian_cookie'] != 1 && $_GET['login'] != 1){	
		printf("<div class='floatright'><button onclick='refreshPage(\"/lendinglibrary.php?login=1\")'>Librarian Login</button></div>\n\n");
	}
	//set login cookie
	if ($_REQUEST['pswd']) {
		if ($_REQUEST['pswd'] == 'SHCCBooks'){
			printf("<script type='text/javascript'> document.cookie = \"librarian_cookie=1\"; window.location = '/lendinglibrary.php'; </script>");
		} else {	
			printf("<script type='text/javascript'> window.location = '/lendinglibrary.php?message=Incorrect+password.&login=1';</script>\n");
		}
	}
	//prints form to login as a librarian.
	if ($_REQUEST['login'] == 1){
		printf("\t <form action='/lendinglibrary.php' method='post' style='float:right;'>\n");
		if ($_REQUEST['message'] == "Incorrect password."){
			printf("<font color='red'>Incorrect Password.</font>\n");
		}
		printf("\t\t<input type='password' name='pswd' placeholder='Password'/>\n");
		printf("\t <input type='Submit' value='login' />\n"); 
		printf("\t <input type='hidden' name='login' value='1' />\n"); 
		printf("</form>\n\n");
	}
	if ($_REQUEST['librarian_cookie'] == 1){
		printf("<div class='floatright'><button onclick='refreshPage(\"lendinglibrary.php?logout=1\")'>Logout</button></div>\n\n");
	}
	//delete login cookie
	if ($_REQUEST['logout'] == 1){
		printf("<script type='text/javascript'>document.cookie = \"librarian_cookie=; expires=Thu, 01-Jan-70 00:00:01 GMT;\";</script>"); 
		printf("<script type='text/javascript'> window.location = \"/officelibrary\"; </script>");
	}
}
function addBook() 
{
	$newTitle = $_REQUEST['newTitle'];
	$newNumCopies = $_REQUEST['newNumCopies'];
	$newAuthor = $_REQUEST['newAuthor'];
	$newGenre = $_REQUEST['newGenre'];	
	$testBook = mysql_fetch_array(mysql_query("SELECT * FROM `SHCCLibrary` WHERE `title`='".$newTitle."' && `author`='".$newAuthor."'"));
	if ((stristr($testBook['title'], $newTitle) != FALSE) && (stristr($testBook['author'], $newAuthor) != FALSE) && ($_GET['skip'] != 1)) {
		printf("<script type='text/javascript'>warnDuplicate(\"%s\",\"%s\",\"%s\",\"%s\");</script>\n", $newTitle, $newAuthor, $newNumCopies, $newGenre);
	} else {
		if ($_GET['addCopy'] == 1){
			addCopy($testBook, $newNumCopies);
		} else {
			mysql_query("INSERT INTO SHCCLibrary (title, author, orig_copies, copies_avail, genre, who_has, checkout, due, email, overDue) VALUES ('" . $newTitle . "','" . $newAuthor . "','" . $newNumCopies . "','" . $newNumCopies . "', '" . $newGenre . "', 'Available', '', '','', '0')");
			if ($newNumCopies > 1){
				$newIdQuery = mysql_query("SELECT * FROM `SHCCLibrary` WHERE `title`='".$newTitle."'");
				$newId = mysql_fetch_array($newIdQuery);
				mysql_query("CREATE TABLE `SHCCLibrary_ ".$newId['id']."` (`id` int(10) unsigned NOT NULL auto_increment, `title` text NOT NULL, `author` text NOT NULL, `orig_copies` int(11) unsigned NOT NULL default '1', `copies_avail` int(4) unsigned NOT NULL default '1', `genre` text NOT NULL, `who_has` text NOT NULL, `checkout` date NOT NULL default '0000-00-00', `due` date NOT NULL default '0000-00-00', `email` text NOT NULL,`overDue` int(4) unsigned NOT NULL default '0', PRIMARY KEY  (`id`))");
				for ($g=1; $g<=$_REQUEST['newNumCopies']; $g++){
					mysql_query("INSERT INTO `SHCCLibrary_ ".$newId['id']."` (title, author, orig_copies, copies_avail, genre, who_has, checkout, due, email, overDue) VALUES ('" . $newTitle . " - copy " . $g . "','" . $newAuthor . "','" . $newNumCopies . "','1','', 'Available', '', '', '', '0')");
				}
			}
			printf("<script type='text/javascript'> window.location = 'http://www.southhillcalvary.org/lendinglibrary.php?message=Added!'; </script>");
		}	
	}
}
function addBookForm ()
{
	if ($_GET['addABook']){
		printf ("\t<br><form action='lendinglibrary.php' method='get'>\n"); 
		printf ("\t\t<input type='text' name='newTitle' placeholder='Title'/>\n");
		printf ("\t\t<input type='text' name='newAuthor' placeholder='Author by last name'/>\n"); 
		printf ("\t\t<select name='newGenre'>\n");
		printf("\t\t <option value='#'>Genre...</option>\n");
		printf("\t\t <option value='Apologetics'>Apologetics</option>\n");
		printf("\t\t <option value='Bible Study'>Bible Study</option>\n");
		printf("\t\t <option value='Biography'>Biography</option>\n");
		printf("\t\t <option value='ChristianLiving'>Christian Living</option>\n");
		printf("\t\t <option value='Classics'>Classics</option>\n");
		printf("\t\t <option value='Commentary'>Commentary</option>\n");
		printf("\t\t <option value='Counseling'>Counseling</option>\n");
		printf("\t\t <option value='CreationScience'>Creation & Science</option>\n");
		printf("\t\t <option value='CultsWorldReligions'>Cults & World Religions</option>\n");
		printf("\t\t <option value='CurrentEvents'>Current Events & End Times</option>\n");
		printf("\t\t <option value='Dating'>Dating</option>\n");
		printf("\t\t <option value='Devotionals'>Devotionals</option>\n");
		printf("\t\t <option value='Marriage'>Marriage</option>\n");
		printf("\t\t <option value='Evangelism'>Evangelism</option>\n");
		printf("\t\t <option value='Fiction'>Fiction</option>\n");
		printf("\t\t <option value='Financial '>Financial</option>\n");
		printf("\t\t <option value='General'>General</option>\n");
		printf("\t\t <option value='HolySpirit'>Holy Spirit</option>\n");
		printf("\t\t <option value='Leadership'>Leadership</option>\n");
		printf("\t\t <option value='MarriageFamilyParenting'>Marriage & Family/Parenting</option>\n");
		printf("\t\t <option value='Men'>Mens</option>\n");
		printf("\t\t <option value='Missions'>Missions</option>\n");
		printf("\t\t <option value='Prayer'>Prayer</option>\n");
		printf("\t\t <option value='Questions'>Questions</option>\n");
		printf("\t\t <option value='Reference'>Reference</option>\n");
		printf("\t\t <option value='AV'>Video & Audio</option>\n");
		printf("\t\t <option value='Women'>Womens</option>\n");
		printf("\t</select>\n"); 	
	 	printf ("\t\t<input type='number' name='newNumCopies' placeholder='# copies'/>\n");
		printf ("\t<input type='submit' value='Add' />\n");
		printf("<button onclick='refreshPage(\"/lendinglibrary.php\")'>Never Mind</button>\n"); 
		printf ("\t </form></br></br>\n\n");
	} else {	
		printf("<button  onclick='refreshPage(\"/lendinglibrary.php?addABook=1\")'>Add a New Book</button>\n\n");
	}
}
function addCopy($book, $newNumCopies) 
{
	if ($book['orig_copies'] == 1){
		$newNumOrigCopies = $book['orig_copies'] + $newNumCopies;
		$newNumCopiesAvail = $book['copies_avail'] + $newNumCopies;
		mysql_query("CREATE TABLE `SHCCLibrary_ ".$book['id']."` (`id` int(10) unsigned NOT NULL auto_increment, `title` text NOT NULL, `author` text NOT NULL, `orig_copies` int(11) unsigned NOT NULL default '1', `copies_avail` int(4) unsigned NOT NULL default '1', `genre` text NOT NULL, `who_has` text NOT NULL, `checkout` date NOT NULL default '0000-00-00', `due` date NOT NULL default '0000-00-00', `email` text NOT NULL, `overDue` int(4) unsigned NOT NULL default '0', PRIMARY KEY  (`id`))");
		for ($g=1; $g<=$newNumOrigCopies; $g++){
			mysql_query("INSERT INTO `SHCCLibrary_ ".$book['id']."` (title, author, orig_copies, copies_avail, genre, who_has, checkout, due, email, overDue) VALUES ('" . $book['title'] . " - copy " . $g . "','" . $book['author'] . "','1','1','', 'Available', '', '', '', '0')");
		}
		if ($book['copies_avail'] == 0) {	
			mysql_query("UPDATE `SHCCLibrary_ ".$book['id']."` SET `copies_avail` = '0' WHERE `title` = '". $book['title']." - copy 1'");
			mysql_query("UPDATE `SHCCLibrary_ ".$book['id']."` SET `who_has` = '".$book['who_has']."' WHERE `title` = '". $book['title']." - copy 1'");
			mysql_query("UPDATE `SHCCLibrary_ ".$book['id']."` SET `checkout` = '".$book['checkout']."' WHERE `title` = '". $book['title']." - copy 1'");
			mysql_query("UPDATE `SHCCLibrary_ ".$book['id']."` SET `due` = '".$book['due']."' WHERE `title` = '". $book['title']." - copy 1'");
			mysql_query("UPDATE `SHCCLibrary_ ".$book['id']."` SET `email` = '".$book['email']."' WHERE `title` = '". $book['title']." - copy 1'");
			mysql_query("UPDATE `SHCCLibrary_ ".$book['id']."` SET `overDue` = '".$book['overDue']."' WHERE `title` = '". $book['title']." - copy 1'");
		}
		mysql_query("UPDATE `SHCCLibrary` SET `orig_copies` = '". $newNumOrigCopies ."' WHERE `id` = '". $book['id'] ."'");	
		mysql_query("UPDATE `SHCCLibrary` SET `copies_avail` = '". $newNumCopiesAvail ."' WHERE `id` = '". $book['id'] ."'");
		printf("<script type='text/javascript'>refreshPage(\"/lendinglibrary.php?message=Book(s) Added!\");</script>\n\n");
	} else {	
		$newNumOrigCopies = $book['orig_copies'] + $newNumCopies;
		$newNumCopiesAvail = $book['copies_avail'] + $newNumCopies;
		for ($g=1; $g<=$newNumCopies; $g++){
			mysql_query("INSERT INTO `SHCCLibrary_ ".$book['id']."` (title, author, orig_copies, copies_avail, genre, who_has, checkout, due, email, overDue) VALUES ('" . $book['title'] . " - copy " . ($g+$book['orig_copies']) . "','" . $book['author'] . "','1','1','', 'Available', '', '', '', '0')");
		}	
		mysql_query("UPDATE `SHCCLibrary` SET `orig_copies` = '". $newNumOrigCopies ."' WHERE `id` = '". $book['id'] ."'");	
		mysql_query("UPDATE `SHCCLibrary` SET `copies_avail` = '". $newNumCopiesAvail ."' WHERE `id` = '". $book['id'] ."'");
		printf("<script type='text/javascript'>refreshPage(\"/lendinglibrary.php?message=Book(s) Added!\");</script>\n\n");
	}
}
function genreManager() 
{	
	$result = mysql_query("SELECT * FROM `SHCCLibrary` ORDER BY `title` DESC");
	$AllGenresAreSet = TRUE;	
	while ($row = mysql_fetch_array($result)) {
		if ($row['genre'] != NULL) continue;
		else {
			$AllGenresAreSet = FALSE;
			break;
		}
	}
	if (!$AllGenresAreSet) {
		if ($_GET["genres"] == "NONE"){
			printf("<button  onclick='refreshPage(\"/lendinglibrary.php\")'>Show All</button>\n\n");
		} else {
			printf("<button  onclick='refreshPage(\"/lendinglibrary.php?genres=NONE\")'>Show All Books Without Genres</button>\n\n");		
		}
	}
}
function printAllOutBooks()
{	
	$checkedOutTest = mysql_query("SELECT * FROM `SHCCLibrary` WHERE `orig_copies`>`copies_avail` ORDER BY `due` ASC");	
	if (mysql_num_rows($checkedOutTest) != 0){
		if (!$_GET['allOut']){
			printf("<button  onclick='refreshPage(\"/lendinglibrary.php?allOut=1\")'>Show All Checked Out Books</button>\n");
		} else {	
			printf("<button  onclick='refreshPage(\"/lendinglibrary.php\")'>Show All</button>");
		}
	}	
}
function printAllOverDue() 
{	
	$today = date("Y-m-d");	
	$overDueTest = mysql_query("SELECT * FROM `SHCCLibrary` WHERE `overDue`='1' ORDER BY `due` ASC");
	if (mysql_num_rows($overDueTest) != 0){
		if (!$_GET['overDue']){
			printf("<button  onclick='refreshPage(\"/lendinglibrary.php?overDue=1\")'>Show All Overdue Books</button>\n");
		} else {	
			printf("<button  onclick='refreshPage(\"/lendinglibrary.php\")'>Show All</button>");
		}
	}
}
function print_lib_table($var) 
{
	$num = mysql_num_rows($var);	
	if ($num == 1) {
		printf ("<div class='floatright'>Displaying %d result</div></br>\n", $num); 
	} else {	
		printf ("<div class='floatright'>Displaying %d results</div></br>\n", $num);
	}
	printf("<table style='width:%s;'>", "100%");
    printf("\t<tr>\n\t\t<td><h3>Title</h3></td><td><h3>Author</h3></td><td><h3>Availability</h3></td>\n\t</tr>");
	while ($row = mysql_fetch_array($var)){
		if ($i){
			printf("\t<tr class='dotted_lines row_odd'>\n");
        } else { 	
        	printf("\t<tr class='dotted_lines row_even'>\n");
		}		
		printf("\t\t<td>%s</td>",$row['title']); 
		printf("<td>%s</td>",$row['author']);  
		if ($row['copies_avail'] >= 1){ 
			if ($row['copies_avail'] == 1){
				printf("<td>1 copy available</td>\n");
			} else {
				printf("<td>%s copies available</td>\n",$row['copies_avail']);				
			}
  		} else { 
			printf("<td>Unavailable at the moment.</td>\n");
		}
		printf("<td style='display:none;'>%s</td>", $row['genre']);
		printf("\t</tr>\n");
        if ($i == 1){
        	$i = 0;
        } else {
        	$i = 1;
        }
	}
	printf("</table>\n\n");
}
function print_librarian_table($var) 
{	
	$num = mysql_num_rows($var);	
	$currDate = strtotime(date(Y).date(m).date(d));
	if ($num == 1) {
		printf ("<div class='floatright'>Displaying %d result</div></br>\n", $num); 
	} else {
		printf ("<div class='floatright'>Displaying %d results</div></br>\n", $num);
	}
	printf ("<table style='width:%s;'>\n", "100%");
    printf ("\t<tr>\n\t\t<td><h3>Title</h3></td><td><h3>Author</h3></td><td><h3>Availability</h3></td>\n\t</tr>\n");
	while ($row = mysql_fetch_array($var)){
		if ($i){
			printf ("\t<tr class='dotted_lines row_odd'>\n");
        } else{
        	printf ("\t<tr class='dotted_lines row_even'>\n");
        }
		printf ("\t\t<td>%s</td>",$row['title']); 
		printf ("<td>%s</td>",$row['author']); 
		if ($row['orig_copies'] >= 2){ 
			if ($row['copies_avail'] == 0){
				printf("<td>no copies available</td>\n");
			} else {
				if ($row['overDue'] == 1){	
					printf("<td><font color='red'>%s copies of %s available</font></td>\n", $row['copies_avail'], $row['orig_copies']);
				} else {
					printf("<td>%s copies of %s available</td>\n", $row['copies_avail'], $row['orig_copies']);
				}				
			}
  		} elseif ($row['copies_avail'] == 1){	
  			printf("<td>1 copy available</td>\n");
  		} else { 
			if ($row['overDue'] == 1){		
				printf("<td>%s, Due: <font color='red'>%s</font></td>\n", $row['who_has'], date("M j, Y", strtotime($row['due'])));
			} else {
				printf("<td>%s, Due: %s</td>\n", $row['who_has'], date("M j, Y", strtotime($row['due'])));
			}
		}
		if ($row['orig_copies'] > 1){
			printf("<td><button onclick='refreshPage(\"/librarydetailmultiple.php?title=%s&id=%s\")'>View Books</button></td>\n\n", urlencode($row['title']), $row['id']);
		} else {
			printf("<td><button onclick='refreshPage(\"/librarydetail.php?title=%s&id=%s\")'>Functions</button></td>\n\n", urlencode($row['title']), $row['id']);
		}
		printf("<td style='display:none;'>%s</td>", $row['genre']);
		printf ("\t</tr>\n");
        if ($i == 1){
        	$i = 0;
        } else {
        	$i = 1;
        }
	}
	printf("</table>");
}
function print_management_table($var) 
{	
	$num = mysql_num_rows($var);	
	$currDate = strtotime(date(Y).date(m).date(d));
	if ($num == 1) {
		printf ("<div class='floatright'>Displaying %d result</div></br>\n", $num); 
	} else {
		printf ("<div class='floatright'>Displaying %d results</div></br>\n", $num);
	}
	printf ("<table style='width:%s;'>\n", "100%");
    printf ("\t<tr>\n\t\t<th><h3>Title</h3></th><th><h3>Author</h3></th><th><h3>Genre</h3></th><th></th>\n\t</tr>\n");
	while ($row = mysql_fetch_array($var)){
		if ($i){
			printf ("\t<tr class='dotted_lines row_odd'>\n");
        
        } else {
        	printf ("\t<tr class='dotted_lines row_even'>\n");
        }
		printf ("\t\t<td>%s</td>",urldecode($row['title'])); 
		printf ("<td>%s</td>",$row['author']);
		printf ("<td>\n");
		if ($row['genre'] == NULL){
		printf("<form style='margin-right:30px;' action='lendinglibrary.php?genre=NONE' method='get' id='%s' >\n",$row['id']);
			printf("\t<select name='setGenre' onchange='submitMe(\"%s\")'>\n", $row['id']);
				printf("\t\t <option value='#'>Set Genre...</option>\n");
					printf("\t\t <option value='Apologetics'>Apologetics</option>\n");
					printf("\t\t <option value='Bible Study'>Bible Study</option>\n");
					printf("\t\t <option value='Biography'>Biography</option>\n");
					printf("\t\t <option value='ChristianLiving'>Christian Living</option>\n");
					printf("\t\t <option value='Classics'>Classics</option>\n");
					printf("\t\t <option value='Commentary'>Commentary</option>\n");
					printf("\t\t <option value='Counseling'>Counseling</option>\n");
					printf("\t\t <option value='CreationScience'>Creation & Science</option>\n");
					printf("\t\t <option value='CultsWorldReligions'>Cults & World Religions</option>\n");
					printf("\t\t <option value='CurrentEvents'>Current Events & End Times</option>\n");
					printf("\t\t <option value='Dating'>Dating</option>\n");
					printf("\t\t <option value='Devotionals'>Devotionals</option>\n");
					printf("\t\t <option value='Marriage'>Marriage</option>\n");
					printf("\t\t <option value='Evangelism'>Evangelism</option>\n");
					printf("\t\t <option value='Fiction'>Fiction</option>\n");
					printf("\t\t <option value='Financial '>Financial</option>\n");
					printf("\t\t <option value='General'>General</option>\n");
					printf("\t\t <option value='HolySpirit'>Holy Spirit</option>\n");
					printf("\t\t <option value='Leadership'>Leadership</option>\n");
					printf("\t\t <option value='MarriageFamilyParenting'>Marriage & Family/Parenting</option>\n");
					printf("\t\t <option value='Men'>Mens</option>\n");
					printf("\t\t <option value='Missions'>Missions</option>\n");
					printf("\t\t <option value='Prayer'>Prayer</option>\n");
					printf("\t\t <option value='Questions'>Questions</option>\n");
					printf("\t\t <option value='Reference'>Reference</option>\n");
					printf("\t\t <option value='AV'>Video & Audio</option>\n");
					printf("\t\t <option value='Women'>Womens</option>\n");
			printf("\t</select>\n");
		printf("  <input type='hidden' name='genres' value='NONE' />\n");
		printf("  <input type='hidden' name='idGenre' value='%s' />\n", $row['id']);
		printf("</form>\n");
		} else {
			printf("%s", $row['genre']);
		}
		if ($row['orig_copies'] > 1){
			printf("<td><button onclick='refreshPage(\"/librarydetailmultiple.php?title=%s&id=%s\")'>View Books</button></td>\n\n", $row['title'], $row['id']);		
		} else {	
			printf("<td><button onclick='refreshPage(\"/librarydetail.php?title=%s&id=%s\")'>Functions</button></td>\n\n", $row['title'], $row['id']);		
		}
		printf ("\t</tr>\n");
        if ($i == 1){
        	$i = 0;
        } else {
        	$i = 1;
        }
	}
	printf("</table>");
	if ($_GET['setGenre'] && $_GET['idGenre']){
		if ($_GET['setGenre'] != "#"){
			mysql_query("UPDATE `SHCCLibrary` SET `genre` ='" . $_GET['setGenre'] . "' WHERE `id`='" . $_GET['idGenre'] . "'");
			printf("<script type='text/javascript'> window.location = '/lendinglibrary.php?genres=NONE'; </script>");
		} else {
			printf("<script type='text/javascript'> window.location = '/lendinglibrary.php?genres=NONE&message=Cannot+set+genre+to:+Set+Genre'; </script>");
		}
	}
}
function printSearchForm()
{	
	printf("<input type='text' id='searchBar' style='width:300px;' placeholder='Search Names or Titles' onkeyup='searchTable(this)'/>\n");
	printf("<span id='clear' style='margin-left:-16px;cursor:pointer;visibility:hidden;' onclick='searchTableFor(\"\")'>x</span>\n");
	printf("&nbsp;<select onchange='searchTableFor(this.options[this.selectedIndex].value)'>\n");
	printf("\t<option value=''>Genres...</option>\n");
	printf("\t<option value='Apologetics'>Apologetics</option>\n");
	printf("\t<option value='Bible Study'>Bible Study</option>\n");
	printf("\t<option value='Biography'>Biography</option>\n");
	printf("\t<option value='ChristianLiving'>Christian Living</option>\n");
	printf("\t<option value='Classics'>Classics</option>\n");
	printf("\t<option value='Commentary'>Commentary</option>\n");
	printf("\t<option value='Counseling'>Counseling</option>\n");
	printf("\t<option value='CreationScience'>Creation & Science</option>\n");
	printf("\t<option value='CultsWorldReligions'>Cults & World Religions</option>\n");
	printf("\t<option value='CurrentEvents'>Current Events & End Times</option>\n");
	printf("\t<option value='Dating'>Dating</option>\n");
	printf("\t<option value='Devotionals'>Devotionals</option>\n");
	printf("\t<option value='Marriage'>Marriage</option>\n");
	printf("\t<option value='Evangelism'>Evangelism</option>\n");
	printf("\t<option value='Fiction'>Fiction</option>\n");
	printf("\t<option value='Financial '>Financial</option>\n");
	printf("\t<option value='General'>General</option>\n");
	printf("\t<option value='HolySpirit'>Holy Spirit</option>\n");
	printf("\t<option value='Leadership'>Leadership</option>\n");
	printf("\t<option value='MarriageFamilyParenting'>Marriage & Family/Parenting</option>\n");
	printf("\t<option value='Men'>Mens</option>\n");
	printf("\t<option value='Missions'>Missions</option>\n");
	printf("\t<option value='Prayer'>Prayer</option>\n");
	printf("\t<option value='Questions'>Questions</option>\n");
	printf("\t<option value='Reference'>Reference</option>\n");
	printf("\t<option value='AV'>Video & Audio</option>\n");
	printf("\t<option value='Women'>Womens</option>\n");
	printf("</select>\n");
}
function printBookTable() 
{
	printSearchForm();
	if (($_REQUEST['librarian_cookie'] == 1) && ($_GET['genres'] == "NONE")){
		$result = mysql_query("SELECT * FROM SHCCLibrary WHERE `genre`='' ORDER BY title ASC ");
		print_management_table($result);
	}  elseif (($_REQUEST['librarian_cookie'] == 1) && ($_GET['allOut'] == 1)){
		$result = mysql_query("SELECT * FROM SHCCLibrary WHERE `orig_copies`>`copies_avail` ORDER BY title ASC ");
		print_librarian_table($result);
	} elseif (($_REQUEST['librarian_cookie'] == 1) && ($_GET['overDue'] == 1)){
		$today = date("Y-m-d");
		$result = mysql_query("SELECT * FROM SHCCLibrary WHERE `overDue`='1' ORDER BY title ASC ");
		print_librarian_table($result);
	} else {
		//it lists them all.	
		if ($_REQUEST['librarian_cookie'] == 1){
			$result = mysql_query("SELECT * FROM SHCCLibrary ORDER BY title ASC ");
			print_librarian_table($result);
		} else { 
			$result = mysql_query("SELECT * FROM SHCCLibrary ORDER BY title ASC ");
			print_lib_table($result);
		}
	}
}
function printLibrarianPanel() 
{
	printf("<div class='content' style='text-align:center;'>\n");
	if ($_REQUEST['message']) printf("<h2 style='color:red;'><b>%s</b></h2><br>", $_REQUEST['message']);
	addBookForm ();
	if ($_REQUEST['newTitle'] && $_REQUEST['newAuthor'] && $_REQUEST['newGenre'] && $_REQUEST['newNumCopies']){
		if (($_REQUEST['newTitle'] != "") && ($_REQUEST['newAuthor'] != "") && ($_REQUEST['newGenre'] != "#") && ($_REQUEST['newNumCopies'] != "")) addBook();
		else printf("<script type=text/javascript>refreshPage(\"/lendinglibrary.php?addABook=1&message=Please+add+a+genre...\")</script>\n");
	} else if ($_REQUEST['newTitle'] && $_REQUEST['newAuthor'] && $_REQUEST['newGenre'] && !$_REQUEST['newNumCopies']){ 
		printf("<script type=text/javascript>refreshPage(\"/lendinglibrary.php?addABook=1&message=Please+specify+the+number+of+copies...\")</script>\n");
	} else if ($_REQUEST['newTitle'] && !$_REQUEST['newAuthor'] && $_REQUEST['newGenre'] && $_REQUEST['newNumCopies']){ 
		printf("<script type=text/javascript>refreshPage(\"/lendinglibrary.php?addABook=1&message=Please+specify+the+author...\")</script>\n");
	} else if (!$_REQUEST['newTitle'] && $_REQUEST['newAuthor'] && $_REQUEST['newGenre'] && $_REQUEST['newNumCopies']){ 
		printf("<script type=text/javascript>refreshPage(\"/lendinglibrary.php?addABook=1&message=Please+specify+a+title...\")</script>\n");
	}
	genreManager();
	printAllOutBooks();
	printAllOverDue();
	printf("</div><br>\n\n");
}
?>